#include"task.h"
#include"loginclient.h"
#include <grp.h>
#include <string.h>
int main(int argc, char *argv[])
{
    // ./client 192.168.72.128 12345
    ARGS_CHECK(argc,3);
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret = connect(sockfd,(struct sockaddr *)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect");
    //三期注册登录
    char username[1024]={0};
    char password[1024]={0};
    int flag;
    train_t train;
    while(1)
    {
    printf("请选择注册or登录（0代表注册，1代表登录）\n");
    scanf("%d",&flag);
    getchar(); 
    send(sockfd,&flag,sizeof(train.length),MSG_NOSIGNAL);
    printf("请输入用户名\n");
    fgets(username,sizeof(username),stdin);
        username[strcspn(username, "\n")] = '\0';
    printf("请输入密码\n");
    fgets(password,sizeof(password),stdin);
        password[strcspn(password, "\n")] = '\0';
    switch(flag)
    {
        //0注册 1登录
        case 0:
        ret=userregister(sockfd,username, password);
        if(ret==-1)
        {
            printf("注册失败\n");
            continue;
        }
        else
        {
            printf("注册成功\n");
        }
        break;
        case 1:
         ret=userlogin(sockfd,username, password);
        if(ret==-1)
        {
            printf("登录失败\n");
            continue;
        }
        else 
        {
            printf("登录成功\n");
            goto end;
        }
        break;
    }
}
end:
    while(1)
    {
        char buf[1024] = {0};
        char result[1024]={0};
        int idx=0;
        printf("请输入指令\n");
        fgets(buf,sizeof(buf),stdin);
        buf[strcspn(buf, "\n")] = '\0';
        char *token=strtok(buf," ");
        while(token!=NULL)
        {
            strcat(result+idx,token);
            idx+=strlen(token);
            token=strtok(NULL," ");
        }
        char arg[256];
        if(strncmp(result,"cd",2)==0)
        {
            strncpy(arg,result+2,sizeof(arg));
            send_command(sockfd,  CD,arg);
            //recv_response(sockfd);
        }
        else if(strcmp(result,"ls")==0)
        {
            send_command(sockfd,  LS , "");
            printf("当前文件目录为：\n");
            recv_response(sockfd);
        }
        else if(strncmp(result,"puts",4)==0)
        {
            strncpy(arg,result+4,sizeof(arg));
            int type = send_command(sockfd,  PUTS,"");
            printf("--------%d\n",type);
            handle_puts(sockfd,  arg);
            //up_load(sockfd,arg);
            //recv_response(sockfd);
        }
        else if(strncmp(result,"gets",4)==0)
        {
            strncpy(arg,result+4,sizeof(arg));
            send_command(sockfd,  GETS,arg);
            down_load(sockfd,arg);
           //recv_response(sockfd);
        }
        else if(strcmp(result ,"pwd")==0)
        {
            send_command(sockfd,  PWD,"");
            printf("当前工作目录文件有：\n");
            recv_response(sockfd);
        }
        else if(strncmp(result,"remove",6)==0)
        {
            strncpy(arg,result+6,sizeof(arg));
            send_command(sockfd,  REMOVE,arg);
            //recv_response(sockfd);
        }
        else if(strncmp(result,"mkdir",5)==0)
        {
            strncpy(arg,result+5,sizeof(arg));
            send_command(sockfd,  MKDIR,arg);
            recv_response(sockfd);
        }
        else if(strncmp(result,"rmdir",5)==0)
        {
            strncpy(arg,result+5,sizeof(arg));
            send_command(sockfd,  RMDIR,arg);
            recv_response(sockfd);
        }
        else 
        {
            printf("Invalid COmmand\n");
        }
    }
    return 0;
}
